/*
 * Copyright 2013 Sigurd Randoll.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.client.controller;

import de.digiway.rapidbreeze.client.config.ServerConfiguration;
import de.digiway.rapidbreeze.client.config.ServerConfigurationEvent;
import de.digiway.rapidbreeze.client.infrastructure.BusEvents;
import de.digiway.rapidbreeze.shared.rest.config.DownloadSpeed;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import org.rribbit.Listener;

/**
 *
 * @author Sigurd
 */
public class SpeedControlController implements Initializable {

    @FXML
    private ToggleButton stopButton;
    @FXML
    private ToggleGroup speedGroup;
    @FXML
    private ToggleButton slowButton;
    @FXML
    private ToggleButton normalButton;
    @FXML
    private ToggleButton fastButton;
    @FXML
    private AnchorPane mainPane;
    private ServerConfiguration serverConfiguration;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initToggleGroup();
        mainPane.setDisable(true);
    }

    void setServerConfiguration(ServerConfiguration serverConfiguration) {
        this.serverConfiguration = serverConfiguration;
    }

    @Listener(hint = BusEvents.SERVER_CONFIGURATION_CHANGED_EVENT)
    public void onSpeedChange(ServerConfigurationEvent event) {
        if (event.getConfigurationKey().equals(ServerConfiguration.PROP_DOWNLOAD_SPEED)) {
            DownloadSpeed speed = event.getNewValue();
            switch (speed) {
                case STOP:
                    stopButton.setSelected(true);
                    break;
                case SLOW:
                    slowButton.setSelected(true);
                    break;
                case NORMAL:
                    normalButton.setSelected(true);
                    break;
                case FAST:
                    fastButton.setSelected(true);
                    break;
            }
        }
    }

    @Listener(hint = BusEvents.SERVER_CONNECTION_CHANGED_EVENT)
    public void onConnectionChange(boolean connected) {
        mainPane.setDisable(!connected);
    }

    @FXML
    private void onNormal(ActionEvent event) {
        serverConfiguration.setDownloadSpeed(DownloadSpeed.NORMAL);
    }

    @FXML
    private void onStop(ActionEvent event) {
        serverConfiguration.setDownloadSpeed(DownloadSpeed.STOP);
    }

    @FXML
    private void onSlow(ActionEvent event) {
        serverConfiguration.setDownloadSpeed(DownloadSpeed.SLOW);
    }

    @FXML
    private void onFast(ActionEvent event) {
        serverConfiguration.setDownloadSpeed(DownloadSpeed.FAST);
    }

    private void initToggleGroup() {
        addToggleGroupEvent(stopButton);
        addToggleGroupEvent(slowButton);
        addToggleGroupEvent(normalButton);
        addToggleGroupEvent(fastButton);
    }

    private void addToggleGroupEvent(final ToggleButton btn) {
        btn.addEventFilter(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (btn.equals(speedGroup.getSelectedToggle())) {
                    mouseEvent.consume();
                }
            }
        });
    }
}
